package com.project.myxima.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.project.myxima.R;
import com.project.myxima.base.BaseApplication;
import com.project.myxima.views.PlayList_PopupWindow;
import com.project.myxima.views.RoundRectImageView;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.InnerHolder> {

    private List<Track> mData = new ArrayList<>();
    private int playIndex = 0;

    PlayList_PopupWindow.onPlayListItemClickListener mItemClickListener = null;

    @NonNull
    @Override
    public PlayListAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_play_list,null);


        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull PlayListAdapter.InnerHolder holder, final int position) {
        Track currentTrack = mData.get(position);
        TextView trackTitle_tv = holder.itemView.findViewById(R.id.track_title_tv);

        //设置正在播放的那条track的字体颜色
        trackTitle_tv.setTextColor(
                BaseApplication.getAppContext().getResources().getColor(playIndex == position ?
                        R.color.main_color : R.color.play_list_text_color)
        );


        trackTitle_tv.setText(currentTrack.getTrackTitle());

        //寻找播放状态的图标
        View playingIconView = holder.itemView.findViewById(R.id.play_icon_iv);
        playingIconView.setVisibility( playIndex == position?
                View.VISIBLE:View.GONE);

        //set the ItemClick event
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.onItemClick(position);//TODO:what for?
            }
        });

        //set the viewPager cover
        ImageView imageView = holder.itemView.findViewById(R.id.track_pager_view);

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(List<Track> tracks){
        mData.clear();
        mData.addAll(tracks);
        notifyDataSetChanged();
    }

    public void setCurrentPlayPosition(int position) {
        this.playIndex = position;
        notifyDataSetChanged();
    }

    public void setItemClickListener(PlayList_PopupWindow.onPlayListItemClickListener listener) {
        mItemClickListener = listener;
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
